package com.youku.plugins.controlbarV4.view
{
   import com.youku.interfaces.IPlayerProxy;
   import com.youku.plugins.controlbarV4.view.components.Previewer;
   import com.youku.plugins.PluginEventDispather;
   import com.youku.plugins.controlbarV4.view.events.PreviewerEvent;
   import com.youku.plugins.controlbarV4.view.events.ControlBarEvent;
   import com.youku.plugins.controlbarV4.ControlBarConstant;
   import com.youku.events.PlayerControlEvent;
   import com.youku.PlayerConfig;
   import com.youku.events.PluginEvent;
   
   public class PreviewerMediator extends Object
   {
      
      public function PreviewerMediator(param1:Previewer, param2:IPlayerProxy, param3:PluginEventDispather) {
         super();
         this._preview = param1;
         this._playerProxy = param2;
         this._pluginEventDispather = param3;
         this._playerProxy.addEventListener(PlayerControlEvent.PLAYER_SIZE_CHANGED,this.onPlayerControlEvent);
         this._playerProxy.addEventListener(PlayerControlEvent.FULLSCREEN_CHANGED,this.onPlayerControlEvent);
         this.onRegister();
      }
      
      private var _playerProxy:IPlayerProxy;
      
      private var _jumpTime:Number = -1;
      
      private var _playerWidth:Number = 0;
      
      private var _preview:Previewer;
      
      private var _pluginEventDispather:PluginEventDispather;
      
      protected function get previewer() : Previewer {
         return this._preview;
      }
      
      private function onRegister() : void {
         this.previewer.addEventListener(PreviewerEvent.PREVIEWER_HOVER,this.onPreviewerEvent);
         this.previewer.addEventListener(PreviewerEvent.PREVIEWER_UNHOVER,this.onPreviewerEvent);
         this.previewer.addEventListener(PreviewerEvent.PREVIEWER_CLICK,this.onPreviewerEvent);
         this.previewer.addEventListener(PreviewerEvent.PREVIEWER_DISAPPEAR,this.onPreviewerEvent);
         this.previewer.addEventListener(PreviewerEvent.PREVIEWER_SHARE,this.onPreviewerEvent);
         this._pluginEventDispather.addEventListener(ControlBarEvent.SHOW_PREVIEWER_PANEL,this.onControlBarEvent);
         this._pluginEventDispather.addEventListener(ControlBarEvent.HIDE_PREVIEWER_PANEL,this.onControlBarEvent);
         this._pluginEventDispather.addEventListener(ControlBarEvent.SHOW_PREVIEWER_VIDEO,this.onControlBarEvent);
         this._pluginEventDispather.addEventListener(ControlBarEvent.HIDE_PREVIEWER_VIDEO,this.onControlBarEvent);
         this._pluginEventDispather.addEventListener(ControlBarEvent.SET_PREVIEW_URL,this.onControlBarEvent);
         this.previewer.visible = false;
      }
      
      private function onControlBarEvent(param1:ControlBarEvent) : void {
         var _loc2_:String = null;
         switch(param1.type)
         {
            case ControlBarEvent.SHOW_PREVIEWER_PANEL:
               this.previewer.setSize(ControlBarConstant.PREVIEW_MODE_TINY,this._playerWidth);
               break;
            case ControlBarEvent.HIDE_PREVIEWER_PANEL:
               break;
            case ControlBarEvent.SHOW_PREVIEWER_VIDEO:
               this.previewer.attachEvent(true);
               this.previewer.setSize(ControlBarConstant.PREVIEW_MODE_SMALL,this._playerWidth);
               break;
            case ControlBarEvent.HIDE_PREVIEWER_VIDEO:
               this.previewer.close();
               this.previewer.setSize(ControlBarConstant.PREVIEW_MODE_TINY,this._playerWidth);
               break;
            case ControlBarEvent.SET_PREVIEW_URL:
               _loc2_ = param1.data.url as String;
               if(param1.data.time != null)
               {
                  this._jumpTime = param1.data.time;
               }
               this.previewer.setVideoURL(_loc2_);
               break;
         }
      }
      
      private function onPlayerControlEvent(param1:PlayerControlEvent) : void {
         var _loc2_:* = NaN;
         var _loc3_:* = false;
         switch(param1.type)
         {
            case PlayerControlEvent.FULLSCREEN_CHANGED:
            case PlayerControlEvent.PLAYER_SIZE_CHANGED:
               _loc2_ = param1.data.width;
               _loc3_ = param1.data.fullscreen;
               this._playerWidth = _loc3_?_loc2_ / ControlBarConstant.CONTROLBAR_SCALE_RATIO:_loc2_;
               this.previewer.playerWidth = this._playerWidth;
               break;
         }
      }
      
      private function onPreviewerEvent(param1:PreviewerEvent) : void {
         var _loc2_:Object = null;
         switch(param1.type)
         {
            case PreviewerEvent.PREVIEWER_HOVER:
               break;
            case PreviewerEvent.PREVIEWER_UNHOVER:
               break;
            case PreviewerEvent.PREVIEWER_CLICK:
               if(!(param1.data.time == -1) || !(this._jumpTime == -1))
               {
                  _loc2_ = {};
                  _loc2_.time = Math.max(param1.data.time,this._jumpTime);
                  _loc2_.jump = true;
                  PlayerConfig.isSeekToKeyframe = true;
                  this._playerProxy.dispatchEvent(new PluginEvent(PluginEvent.CONTROL_BAR_SEEK,_loc2_));
               }
               break;
            case PreviewerEvent.PREVIEWER_DISAPPEAR:
               this._pluginEventDispather.dispatchEvent(new ControlBarEvent(ControlBarEvent.HIDE_PREVIEWER_PANEL));
               break;
            case PreviewerEvent.PREVIEWER_SHARE:
               this._playerProxy.dispatchEvent(new PluginEvent(PluginEvent.CONTROL_PREVIEW_SHARE,param1.data));
               break;
         }
      }
   }
}
